package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.cmd.PouchCMD;
import com.glowdayz.glowmee.dao.PouchCategoryDAO;
import com.glowdayz.glowmee.dao.PouchDAO;

public class ListUserPouchBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListUserPouchBC() {
        super();
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		} else {
			where = "";
		}
		
		String nickName = request.getParameter("nickName");
		String pouchCategory = request.getParameter("pouchCategory");
		if (pouchCategory != null) {
			pouchCategory = Util.getInstance().incoding(pouchCategory);
		}
		String start_date = request.getParameter("start_date");
		String end_date = request.getParameter("end_date");
		if (nickName != null && !nickName.equals("")) {
			nickName = Util.getInstance().incoding(nickName);
			where += " AND r.nickName like '%" + nickName + "%'";
		}
		if (pouchCategory != null && !pouchCategory.equals("- 선택하세요 -")) {
			where += " AND p.idPouchCategory = " + pouchCategory;
		}
		if (start_date != null && !start_date.equals("") && end_date != null && !end_date.equals("")) {
			start_date = start_date.replace("-", "");
			start_date += "000000";
			end_date += "999999";
			end_date = end_date.replace("-", "");
			where += " AND p.create_date BETWEEN " + start_date + " AND " + end_date;
		}
		
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		
		if (order != null && order.equals("isUnPouchCategory")) {
			order = "p.idPouchCategory ASC, p.create_date DESC";
			request.setAttribute("order", "&order=isUnPouchCategory");
		} else if (order != null && order.equals("isPouchCategory")) {
			order = "p.idPouchCategory DESC, p.create_date DESC";
			request.setAttribute("order", "&order=isPouchCategory");
		}
		
		if (order != null && order.equals("isUnRecommend")) {
			order = "p.recommendCount ASC, p.create_date DESC";
			request.setAttribute("order", "&order=isUnRecommend");
		} else if (order != null && order.equals("isRecommend")) {
			order = "p.recommendCount DESC, p.create_date DESC";
			request.setAttribute("order", "&order=isRecommend");
		}
		
		if (order != null && order.equals("isUnComment")) {
			order = "count2 ASC, p.create_date DESC";
			request.setAttribute("order", "&order=isUnComment");
		} else if (order != null && order.equals("isComment")) {
			order = "count2 DESC, p.create_date DESC";
			request.setAttribute("order", "&order=isComment");
		}
		
		if (order != null && order.equals("isUnDisplay")) {
			order = "p.isDisplay ASC, p.create_date DESC";
			request.setAttribute("order", "&order=isUnDisplay");
		} else if (order != null && order.equals("isDisplay")) {
			order = "p.isDisplay DESC, p.create_date DESC";
			request.setAttribute("order", "&order=isDisplay");
		}
		
		String id_str = request.getParameter("id");
		String isDisplay_str = request.getParameter("isDisplay");
		if (id_str != null && isDisplay_str != null) {
			int id = Integer.parseInt(id_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			
			try {
				PouchDAO.getInstance().isDisplayPouch(new PouchCMD(id, isDisplay));
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			if (!where.equals("")) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("pouchCategory", PouchCategoryDAO.getInstance().getPouchCategoryList(1, -1, "pouchCategoryText"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("userPouch", PouchDAO.getInstance().getUserPouchList(page, 10, order, where));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"pouch p, register r", "p.idPouch", "p.idRegister=r.idRegister AND r.userType='USER' AND p.isDisplay='1'" + where));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"pouch p, register r", "p.idPouch", "p.idRegister=r.idRegister AND r.userType='USER' AND p.isDisplay='0'" + where));
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/listUserPouch.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}